﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.UserCode;
namespace SharePointOnlineCustomWorkflowActions
{
    class AddAlertForUser
    {
        public Hashtable AddAlert(SPUserCodeWorkflowContext context,SPUser user)
        {
            Hashtable results = new Hashtable();
            try
            {
                using (SPSite site = new SPSite(context.CurrentWebUrl))
                {
                   
                    SPAlert newAlert = user.Alerts.Add();
                    SPList list = site.OpenWeb(context.WebUrl).Lists[context.ListId];
                    newAlert.AlertType = SPAlertType.Item; 
                    newAlert.Item =  list.GetItemById (context.ItemId) ;
                    newAlert.AlertFrequency = SPAlertFrequency.Immediate;   

                }
                results["success"] = true;
                results["exception"] = string.Empty;
            }
            catch (Exception e)
            {
                results = new Hashtable();
                results["exception"] = e.ToString();
                results["success"] = false;
            }
            return results;
        }
    }
}
